<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla controller library
jimport('joomla.application.component.controller');

/**
 * FAQ Book Component Controller
 */
class ManagerControllerManager extends JController {

	var $urldirect = "";
	var $mainframe = "";

	function __construct() {
		parent::__construct();

		$this->view = & $this->getView('manager', 'html');
		$this->urldirect = 'index.php?option=com_manager&c=manager';
		$this->mainframe = JFactory::getApplication();
	}

	function display() {
		$this->view->display();
	}

	function add() {
		ManagerHelper::showToolBarForm('Add New Serial');
		$this->view->addForm();
	}

	function edit() {
		ManagerHelper::showToolBarForm('Edit Serial');
		$this->view->addForm();
	}

	function cancel() {
		$this->mainframe->redirect($this->urldirect);
	}

	function save() {
		$row = & JTable::getInstance('Serial', 'Table');
		$post = JRequest::get('post', JREQUEST_ALLOWHTML);

		if (isset($post['status'])) {
			if ($post['status'] == 'on') {
				$post['status'] = ManagerHelper::SERIAL_STATUS_USED;
			} else {
				$post['status'] = ManagerHelper::SERIAL_STATUS_NOT_USE;
			}
		} else {
			$post['status'] = ManagerHelper::SERIAL_STATUS_NOT_USE;
		}
		
		if (empty($post['id'])) { // if this is action add
			$today = date("Y-m-d H:i:s");
			$post['generated_date'] = $today;
		}
		
		$row->save($post);
		$this->mainframe->redirect($this->urldirect);
	}
	
	function delete() {
		$row = & JTable::getInstance('Serial', 'Table');
		
		$cid = JRequest::getVar('cid');
		if(!empty($cid)) {
			$row->deleteAll($cid);
		}
		
		$this->mainframe->redirect($this->urldirect);
	}			

	function toExcel() {
		ManagerHelper::formatExcel('Serial');
	}
	
	function toPDF() {
		ManagerHelper::formatPdf('Serial');		
	}
	
	function toWord() {
		
	}
}